const db = require('../db/sql');

exports.addArt = (req, res) => {
  if (!req.file || req.file.fieldname !== 'cover_img')
    return res.send({ status: 1, message: '文章封面是必选参数！' });

  const path = require('path');

  const articleInfo = {
    ...req.body,

    cover_img: path.join('/uploads', req.file.filename),
    pub_date: new Date(),
    author_id: req.user.id,
  };
  const sqlStr = 'insert into ev_articles set ?';

  db.query(sqlStr, articleInfo, (err, results) => {
    if (err)
      return res.send({
        success: false,
        message: err,
      });

    if (results.affectedRows !== 1)
      return res.send({
        success: false,
        message: '发布文章失败！',
      });

    res.send({
      success: true,
      message: '发布文章成功',
    });
  });
};
